package chat.network.http;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by 凯 on 2015/10/24.
 */
public class SendRoomMessage {

    Delegate delegate;
    RoomMessage roomMessage;

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void setRoomMessage(RoomMessage roomMessage) {
        this.roomMessage = roomMessage;
    }

    // 发送
    public void send() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://"+IP+"/Chat/SendRoomMessages";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        if (delegate != null)
                            delegate.onResponse(response,roomMessage);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (delegate != null)
                            delegate.onError(error);
                    }
                }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                Gson gson = new Gson();
                map.put("data",gson.toJson(roomMessage));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    public static interface Delegate {
        public void onResponse(String s, RoomMessage roomMessage);
        public void onError(VolleyError volleyError);
    }

    public static class RoomMessage {
        private String room_ID;
        private String user_ID;
        private String rm_Content;
        private String rm_Time;
        private String rm_Type;

        public String getRoom_ID() {
            return room_ID;
        }
        public void setRoom_ID(String room_ID) {
            this.room_ID = room_ID;
        }
        public String getUser_ID() {
            return user_ID;
        }
        public void setUser_ID(String user_ID) {
            this.user_ID = user_ID;
        }
        public String getRm_Content() {
            return rm_Content;
        }
        public void setRm_Content(String rm_Content) {
            this.rm_Content = rm_Content;
        }
        public String getRm_Time() {
            return rm_Time;
        }
        public void setRm_Time(String rm_Time) {
            this.rm_Time = rm_Time;
        }
        public String getRm_Type() {
            return rm_Type;
        }
        public void setRm_Type(String rm_Type) {
            this.rm_Type = rm_Type;
        }
    }

    public static class ResponseMessageState {
        private String status;

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }
    }

}
